package cn.hchaojie.javase.day08;

import java.util.Timer;
import java.util.TimerTask;

import org.junit.Test;

public class DemoTimerTask {
	@Test
	public void test1() throws InterruptedException {
		Timer timer = new Timer();
		
		timer.schedule(new Task2(), 0, 2000);
		
		System.out.println("测试的线程跑到这里了。。。");
		
		Thread.sleep(10000);
	}

	@Test
	public void test2() throws InterruptedException {
		Timer timer = new Timer();
		
		timer.schedule(new MyTask(), 0, 2000);
		
		System.out.println("测试的线程跑到这里了。。。");
		
		Thread.sleep(10000);
	}
	
	@Test
	public void test3() throws InterruptedException {
		Timer timer = new Timer();
		
		// 匿名内部类
		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				System.out.println("匿名内部类");
			}
		};
		
		timer.schedule(task, 1000);
		
		Thread.sleep(2000);
	}
	
	@Test
	public void test4() throws InterruptedException {
		Timer timer = new Timer();
		
		// 匿名内部类
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				System.out.println("匿名内部类。。。");
			}
		}, 1000);
		
		Thread.sleep(2000);
	}
	
	class MyTask extends TimerTask {
		@Override
		public void run() {
			System.out.println("内部类的任务。。。");
		}
	}
}

class Task2 extends TimerTask {
	@Override
	public void run() {
		System.out.println("任务运行中！");
	}
}
